package com.lichi.zabbix.controller;

import com.lichi.zabbix.auth.UserDO;
import com.lichi.zabbix.entity.ZabbixHost;
import com.lichi.zabbix.response.*;
import com.lichi.zabbix.service.ZabbixService;
import com.lichi.zabbix.util.BusinessWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lichi
 * @create 2020-05-27 14:02
 */
@Api(tags = "zabbix外部公共接口")
@RestController
public class ZabbixController {

    @Autowired
    private ZabbixService zabbixService;

    @ApiOperation("服务初始化")
    @PostMapping("/init")
    public void init() {
        zabbixService.init();
    }

    @ApiOperation("获取版本号")
    @GetMapping("/getApiVersion")
    public String getApiVersion() {
        return zabbixService.getApiVersion();
    }

    @ApiOperation("根据hostid获取主机信息")
    @GetMapping("/getHost/{hostid}")
    public ZabbixResponseHost getHost(String hostid) {
        return zabbixService.getHost(hostid);
    }

    @ApiOperation("创建用户")
    @PostMapping("/createUser")
    public BusinessWrapper<Boolean> createUser(@RequestBody UserDO userDO) {
        userDO.setUsername("lichi");
        userDO.setDisplayName("dixn");
        userDO.setPwd("123456");
        userDO.setMail("2291828485@qq.com");
        return zabbixService.createUser(userDO);
    }

    @ApiOperation("根据用户组名称获取用户组信息")
    @GetMapping("/getUsergroup/{usergroup}")
    public ZabbixResponseUsergroup getUsergroup(@PathVariable String usergroup) {
        return zabbixService.getUsergroup(usergroup);
    }

    @ApiOperation("根据主机组名称获取主机组信息")
    @GetMapping("/getHostgroup/{name}")
    public ZabbixResponseHostgroup getHostgroup(@PathVariable String name) {
        return zabbixService.getHostgroup(name);
    }

    @ApiOperation("创建主机组")
    @PostMapping("/createHostgroup")
    public String createHostgroup(@RequestBody ZabbixHost zabbixHost) {
        return zabbixService.createHostgroup(zabbixHost.getHost());
    }

    @ApiOperation("获取所有主机模板")
    @GetMapping("/queryTemplates")
    public List<ZabbixResponseTemplate> queryTemplates() {
        return zabbixService.queryTemplates();
    }

    @ApiOperation("根据模板名称获取主机模板")
    @GetMapping("/getTemplate/{name}")
    public ZabbixResponseTemplate getTemplate(@PathVariable String name) {
        return zabbixService.getTemplate(name);
    }

    @ApiOperation("创建主机")
    @GetMapping("/createHost/{name}")
    public boolean createHost(@PathVariable String name) {
        return zabbixService.createHost(name);
    }

    @ApiOperation("根据主机号获取监控项目")
    @GetMapping("/queryItems/{hostId}")
    public List<ZabbixResponseItem> queryItems(@PathVariable String hostId) {
        return zabbixService.queryItems(hostId);
    }

    @ApiOperation("获取所有主机信息")
    @GetMapping("/getHost")
    public List<ZabbixResponseHost> getHost() {
        return zabbixService.getHost();
    }
}